/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.desktop.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.desktop.entity.DeptSync;
import org.springblade.desktop.entity.UserSync;
import org.springblade.desktop.mapper.DeptSyncMapper;
import org.springblade.desktop.mapper.UserSyncMapper;
import org.springblade.desktop.service.IUserSyncService;
import org.springblade.desktop.utils.MianUtils;
import org.springblade.desktop.vo.MyResult;
import org.springblade.desktop.vo.UserSyncVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-09-18
 */
@Service
public class UserSyncServiceImpl extends ServiceImpl<UserSyncMapper, UserSync> implements IUserSyncService {

	@Autowired
	private DeptSyncMapper deptSyncMapper;

	@Override
	public IPage<UserSyncVO> selectUserSyncPage(IPage<UserSyncVO> page, UserSyncVO userSync) {
		return page.setRecords(baseMapper.selectUserSyncPage(page, userSync));
	}

	//人员  部门 全查
	@Override
	@Transactional
	public int insertUser(String accessToken) {
		try {
			List<DeptSync> list = deptSyncMapper.selectList(null);
			List<UserSync> userSyncList=new ArrayList<>();
			for(DeptSync deptlist:list){
				MyResult myResult = MianUtils.userPoUtile("https://oapi.dingtalk.com/topapi/v2/user/list", accessToken,
					new HashMap<String, Object>() {{put("DeptId", deptlist.getDeptId());put("Cursor", "0");put("Size", "100");}});
				if ("ok".equals(myResult.getErrmsg())) {
					String result = myResult.getResult();
					JSONObject jsonObject = JSONObject.parseObject(result);
					JSONArray s = (JSONArray) jsonObject.get("list");
					String jsonString = s.toJSONString();
					List<UserSync> userSyncs = JSONArray.parseArray(jsonString, UserSync.class);
					for (UserSync user:userSyncs){
						user.setName(user.getName().replace("[","").replace("]",""));
						if(user!=null && user.getDeptIdLists()!=null && !user.getDeptIdLists().isEmpty()){
							user.setDeptIdList(deptlist.getDeptId());
						}
					}
					userSyncList.addAll(userSyncs);
				}
			}
			boolean saveBatch = this.saveBatch(userSyncList);
			return saveBatch?1:-1;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -2;
		}
	}
}
